/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: 
 * 系统版本: 1.0.0
 * 开发人员: zhangfb
 * 开发时间: 2019/3/31 10:13
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.hyacinth;

import com.hyacinth.domain.DBDAQTaskVo;
import com.hyacinth.task.callback.TaskCallback;
import com.hyacinth.task.callback.WorkCallback;
import com.hyacinth.task.handler.WorkHandler;
import com.hyacinth.task.manager.WorkerManager;
import com.hyacinth.task.worker.Tasker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * 数据库作业处理
 * @author zhangfb
 * @version 1.0.0.1
 * @since JDK 1.8
 */
public class DBWorkHandler implements WorkHandler<DBDAQTaskVo> {

    private final Logger LOGGER = LoggerFactory.getLogger(getClass());

    @Override
    public String startWorker(DBDAQTaskVo workParams, WorkCallback<DBDAQTaskVo> workCallback) throws Exception {
        WorkerManager<DBDAQTaskVo> workerManager = WorkerManager.getInstance();
        String workId = workerManager.createWorker(workParams, workCallback);

        // 判断定时任务是否需要立即执行一次
        Integer immed = workParams.getImmedExecute();
        if (immed != null && immed == 1) {
            workerManager.work(workId, new Tasker<DBDAQTaskVo>(workParams) {
                @Override
                public void run() {
                    // 执行任务
                    execute(workId, getTaskCallback(), workParams);
                }
            }, 0);
        }

        // 判断定时任务是否需要定时执行需求
        Date firstExecuteTime = workParams.getFirstExecuteTime();
        if (firstExecuteTime != null) {
            // 时间间隔, 判断是否循环执行
            Long intervalTime = workParams.getIntervalTime();
            if (intervalTime != null && intervalTime > 0) {
                // 定时并循环执行任务
                workerManager.workAtFixedRate(workId, new Tasker<DBDAQTaskVo>(workParams) {
                    @Override
                    public void run() {
                        // 执行任务
                        execute(workId, getTaskCallback(), workParams);
                    }
                }, firstExecuteTime, intervalTime);
            } else {
                // 定时不循环执行任务
                workerManager.work(workId, new Tasker<DBDAQTaskVo>(workParams) {
                    @Override
                    public void run() {
                        // 执行任务
                        execute(workId, getTaskCallback(), workParams);
                    }
                }, firstExecuteTime);
            }
        }
        return null;
    }

    /**
     * 任务执行
     * @param workId       作业id
     * @param taskCallback 任务回调
     * @param workParams   作业参数
     */
    private void execute(String workId, TaskCallback<DBDAQTaskVo> taskCallback, DBDAQTaskVo workParams) {
        try {
            System.out.println("数据采集任务执行中。。。。。。");
        } catch (Exception e) {
            LOGGER.error("任务执行出错", e);
        }
    }

    @Override
    public int pauseWorker(String workId) throws Exception {
        return 0;
    }

    @Override
    public int resumeWorker(String workId) throws Exception {
        return 0;
    }

    @Override
    public int stopWorker(String workId) throws Exception {
        return 0;
    }
}
